﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Devil : MonoBehaviour {

    public int hp = 100;
    public int maxhp = 100;

    public float speed = 40f;
    public float attackRangeX = 50;
    public float attackRangeY = 25;

    [System.NonSerialized]
    public int directionX = 1;

    public GameObject attackTarget; //攻击目标
    public GameObject hitEffect; //被击特效

    protected Animator target_anim;
    protected AudioSource aud;
    public AudioClip[] onHit_auds;
    public AudioClip die_aud;
    protected Collider2D coder;
    // Use this for initialization
    void Start () {
        target_anim = GetComponent<Animator>();
        aud = GetComponent<AudioSource>();
        coder = GetComponent<Collider2D>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        float HitterX = collision.gameObject.transform.position.x;
        float myX = transform.position.x;
        if (HitterX > myX)
        {
            transform.localScale = new Vector2(-1, 1);
        }
        else
        {
            transform.localScale = new Vector2(1, 1);
        }
        GameObject hitef = GameObject.Instantiate(hitEffect, transform.position, Quaternion.identity);
        hitef.transform.parent = this.transform;
        hp -= 12;
        if (hp <= 0)
        {
            Destroy(coder);
            target_anim.SetTrigger("Die");
        }
        else
        {
            target_anim.SetTrigger("OnHit");
        }
    }

    // Update is called once per frame
    void Update () {
        Debug.Log(hp);
	}

    public void PlayHitSound()
    {
        int index = Random.Range(0, 2);
        aud.PlayOneShot(onHit_auds[index]);
    }
    public void PlayDieSound()
    {
        aud.PlayOneShot(die_aud);
    }
    public void DestroySelf()
    {
        Destroy(this.gameObject);
    }
}
